package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：daqiang
 * @date ：Created in 2019/11/22
 * @description ：
 * @version: 1.0
 */

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> stringList = POIUtils.readExcel(excelFile);
            //把string数组转成ordersetting对象
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] strings : stringList) {
                //一个数组对应一个ordersetting对象
                OrderSetting orderSetting = new OrderSetting();
                //把字符串日期转换成日期类型
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                Date date = sdf.parse(strings[0]);
                //预约日期
                orderSetting.setOrderDate(date);
                //可预约人数
                orderSetting.setNumber(Integer.parseInt(strings[1]));

                //添加到集合中
                orderSettingList.add(orderSetting);
            }
            //把ordersettingList保存到数据库中
            orderSettingService.addOrderSettings(orderSettingList);
            return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/findByMonth")
    public Result findByMonth(String month){
        try {
            List<OrderSetting> orderSettingList = orderSettingService.findByMonth(month);
            //转换结构
            List<Map<String,Object>> mapList = new ArrayList<>();
            //循环得到的数据
            for (OrderSetting orderSetting : orderSettingList) {
                //一个ordersetting对应一个map集合
                Map<String,Object> map = new HashMap<>();
                SimpleDateFormat sdf = new SimpleDateFormat("dd");
                String date = sdf.format(orderSetting.getOrderDate());

                map.put("date",date);
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());
                //添加到集合中
                mapList.add(map);
            }
            return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody OrderSetting orderSetting){
        try {
            List<OrderSetting> orderSettingList = new ArrayList<>();
            orderSettingList.add(orderSetting);
            orderSettingService.addOrderSettings(orderSettingList);
            return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ORDERSETTING_FAIL);
        }
    }
}
